package cn.stylefeng.guns.modular.tool.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicCustomer;
import cn.stylefeng.guns.modular.basic.service.BasicCustomerService;
import cn.stylefeng.guns.modular.tool.entity.ToolInstall;
import cn.stylefeng.guns.modular.tool.model.params.ToolInstallParam;
import cn.stylefeng.guns.modular.tool.service.ToolInstallService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 计件工资设置控制器
 */
@Controller
@RequestMapping("/toolInstall")
public class ToolInstallController extends BaseController {

    private String PREFIX = "/tool/toolInstall";

    @Autowired
    private ToolInstallService toolInstallService;

    @Autowired
    private BasicCustomerService basicCustomerService;

    @Autowired
    private UserService userService;

    /**
     * 跳转到主页面
     */
    @RequestMapping("")
    public String index(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	model.addAttribute("customerlist", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/toolInstall.html";
    }

    /**
     * 新增页面
     */
    @RequestMapping("/add")
    public String add(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	model.addAttribute("customerlist", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/toolInstall_add.html";
    }

    /**
     * 编辑页面
     */
    @RequestMapping("/edit")
    public String edit(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	model.addAttribute("customerlist", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/toolInstall_edit.html";
    }

    /**
     * 新增接口
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ToolInstallParam toolInstallParam) {
        BasicCustomer basicCustomer = basicCustomerService.getById(toolInstallParam.getCustomerId());
        toolInstallParam.setCustomerName(basicCustomer.getCustomerName());
    	if(!toolInstallParam.getGoodsMode().equals("区间")) {
    		toolInstallParam.setGNum(toolInstallParam.getV1());
    		toolInstallParam.setGNum2(null);
    	}
    	if(!toolInstallParam.getOrderMode().equals("区间")) {
    		toolInstallParam.setONum(toolInstallParam.getV2());
    		toolInstallParam.setONum2(null);
    	}
    	toolInstallParam.setDeptId(userService.getDeptId());
        this.toolInstallService.add(toolInstallParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ToolInstallParam toolInstallParam) {
        BasicCustomer basicCustomer = basicCustomerService.getById(toolInstallParam.getCustomerId());
        toolInstallParam.setCustomerName(basicCustomer.getCustomerName());
    	if(!toolInstallParam.getGoodsMode().equals("区间")) {
    		toolInstallParam.setGNum(toolInstallParam.getV1());
    		toolInstallParam.setGNum2(null);
    	}
    	if(!toolInstallParam.getOrderMode().equals("区间")) {
    		toolInstallParam.setONum(toolInstallParam.getV2());
    		toolInstallParam.setONum2(null);
    	}
    	toolInstallParam.setDeptId(userService.getDeptId());
        this.toolInstallService.update(toolInstallParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ToolInstallParam toolInstallParam) {
        this.toolInstallService.delete(toolInstallParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ToolInstallParam toolInstallParam) {
        ToolInstall detail = this.toolInstallService.getById(toolInstallParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(ToolInstallParam toolInstallParam,String condition ) {
    	User user = userService.getcurrentUser();
    	toolInstallParam.setDeptId(user.getDeptId());
        return this.toolInstallService.findPageBySpec(toolInstallParam,condition,user.getOperateId());
    }

}


